<?php


namespace app\api\controller;


use app\admin\model\System\SysPayModel;
use app\api\model\User\UserModel;
use app\Utils\AliPayUtil;
use app\Utils\Util;
use app\Utils\WxPayUtil;
use Exception;
use think\App;
use think\facade\Db;

class AppController extends BaseController
{
    protected $user;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $param = $app->request->param();
        /*if(!empty($param['token'])){
            $user = UserModel::getUserWhere([['token','=',$param['token']]]);
            if(!empty($user)){
                $this->user = $user;
            }else{
                echo json_encode(['code'=>self::CODE_ERROR,'msg'=>"token已过期"]);
                exit();
            }
        }else{
            echo json_encode(['code'=>self::CODE_ERROR,'msg'=>"token无效"]);
            exit();
        }*/
        if (!empty($param['token'])) {
            $user = UserModel::getUserWhere([['token', '=', $param['token']]]);
            if (!empty($user)) {
                $this->user = $user;
            }
        }
    }

    /**
     * @param $user_id
     * @param $user
     * @return array
     *
     */
    public function users($user_id, $user)
    {
        if ($user_id == 0) {
            if (!$user) {
                return ['code' => self::CODE_LOGIN, 'data' => null, 'msg' => "token已过期"];
            } else {
                return $user['id'];
            }
        } else {
            return $user_id;
        }
    }

    /**
     * 余额支付
     * @param string $order_sn 订单号
     * @param float $total_fee 支付金额
     * @param int $type 0: 普通订单 1：合伙人
     * @return array
     */
    public function balancePay($order_sn, $total_fee, $type = 0)
    {
        Db::startTrans();
        try {
            if (empty($type)) {
                //普通订单支付

            } else {
                //合伙人支付

            }
        } catch (Exception $e) {
            Db::rollback();
            return ['code' => self::CODE_ERROR, 'data' => null, 'msg' => "支付失败！"];
        }
    }

    /**
     * 微信发起支付
     * @param string $way 微信支付方式
     * @param float $total_fee 支付金额
     * @param string $out_trade_no 支付订单号out_trade_no
     * @param string $body 内容
     * @param int $type 0: 普通订单 1：合伙人
     * @return array
     */
    public function weChatPay($way, $total_fee, $out_trade_no, $body, $type = 0)
    {
        $wx = SysPayModel::getSysPayWhere([['id', '=', 3]]);
        $extend_json = json_decode($wx['extend_json'], true);

        $appId = $extend_json['app_id'];
        $secret = $extend_json['app_secret'];
        $mchId = $extend_json['mch_id'];
        $key = $extend_json['key'];
        if (empty($type)) {
            $notify_url = $extend_json['notify_url'];
        } else {
            $notify_url = $extend_json['partner_notify_url'];
        }

        //参数
        $total_fee = 0.01;

        $weChat_pay = new WxPayUtil($way, $appId, $secret, $mchId, $key, $out_trade_no, $body, $total_fee * 100, $notify_url);
        $result_pay = $weChat_pay->payOrder();
        if ($result_pay['code'] == 0) {
            return ['code' => self::CODE_SUCCESS, 'data' => $result_pay['data'], 'msg' => $result_pay['msg']];
        } else {
            return ['code' => self::CODE_ERROR, 'data' => null, 'msg' => $result_pay['msg']];
        }
    }

    /**
     * 支付宝发起支付
     * @param float $total_fee 支付金额
     * @param string $out_trade_no 支付订单号out_trade_no
     * @param string $body 内容
     * @param $type 0: 普通订单 1：合伙人
     * @return array
     */
    public function aliPay($total_fee, $out_trade_no, $body, $type = 0)
    {
        $zfb = SysPayModel::getSysPayWhere([['id', '=', 2]]);
        $extend_json = json_decode($zfb['extend_json'], true);

        $appId = $extend_json['app_id'];
        $private_key = $extend_json['private_key'];
        $public_key = $extend_json['public_key'];
        $buyer_id = "";
        $key = $extend_json['key'];
        if (empty($type)) {
            $notify_url = $extend_json['notify_url'];
        } else {
            $notify_url = $extend_json['partner_notify_url'];
        }

        //参数
        $total_fee = 0.01;
        $options = [
            'out_trade_no' => $out_trade_no,
            'body' => $body,
            'total_fee' => $total_fee,
            'notify_url' => $notify_url,
            'buyer_id' => $buyer_id
        ];

        $ali_pay = new AliPayUtil($appId, $private_key, $public_key, $key, $options);
        $result_data = $ali_pay->payOrder();
        if ($result_data['code'] == 0) {
            return ['code' => self::CODE_SUCCESS, 'data' => $result_data['data'], 'msg' => $result_data['msg']];
        } else {
            return ['code' => self::CODE_ERROR, 'data' => null, 'msg' => $result_data['msg']];
        }
    }

    /**
     * 支付宝发起退款
     * @param float $total_fee 支付金额
     * @param string $trade_no 支付交易号 trade_no
     * @return array
     */
    public function aliRefund($total_fee, $trade_no)
    {
        $zfb = SysPayModel::getSysPayWhere([['id', '=', 2]]);
        $extend_json = json_decode($zfb['extend_json'], true);

        $appId = $extend_json['app_id'];
        $private_key = $extend_json['private_key'];
        $public_key = $extend_json['public_key'];
        $key = $extend_json['key'];
        $notify_url = $extend_json['refund_notify_url'];

        //参数
        $options = [
            'trade_no' => $trade_no,
            'total_fee' => $total_fee,
            'notify_url' => $notify_url
        ];

        $ali_pay = new AliPayUtil($appId, $private_key, $public_key, $key, $options);
        $result_data = $ali_pay->refundOrder();
        if ($result_data['code'] == 0) {
            return ['code' => self::CODE_SUCCESS, 'data' => $result_data['data'], 'msg' => $result_data['msg']];
        } else {
            return ['code' => self::CODE_ERROR, 'data' => null, 'msg' => $result_data['msg']];
        }
    }

}
